package barbershop.com.controller;

import barbershop.com.entity.Admin;
import barbershop.com.service.AdminService;
import barbershop.com.service.TokenService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sys")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private TokenService tokenService;

    @RequestMapping("/login")
    public Object Login(@RequestParam(required = false)String aName,
                        @RequestParam(required = false)String aPwd){
        JSONObject jsonObject=new JSONObject();
        Admin admin=adminService.login(aName, aPwd);
        if(admin == null){
            jsonObject.put("message","登录失败,系统管理员不存在");
            return jsonObject;
        }else {
            if (!admin.getaPwd().equals(aPwd)){
                jsonObject.put("message","登录失败,密码错误");
                return jsonObject;
            }else {
                String token = tokenService.getAdminToken(admin);
                jsonObject.put("token", token);
                jsonObject.put("cashier", admin);
                return jsonObject;
            }
        }
    }
}
